package cn.xxc.basic.controller;

import cn.xxc.basic.dto.LoginDto;
import cn.xxc.basic.exception.BusinessException;
import cn.xxc.basic.util.AjaxResult;
import cn.xxc.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 *  登录控制层
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map=logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            return new AjaxResult(false, e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙请稍后重试！");
        }
    }

    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙请稍后重试！");
        }
    }

    @PostMapping("wechat/binder")
    public AjaxResult binder(@RequestBody Map<String, String> map ){
        try {
            return logininfoService.binder(map);
        }catch (BusinessException e){
            return new AjaxResult(false, e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙请稍后重试！");
        }
    }
}
